package com.otitan.la.forest.industry.handler;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.otitan.la.forest.industry.entity.Cat;
import com.otitan.la.forest.industry.entity.CatInputEntity;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Description: java类作用描述
 * @Author: jiachangsen
 * @CreateDate: 2020/10/15 09:02
 * @UpdateUser: jiachangsen
 * @UpdateDate: 2020/10/15 09:02
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class CatVerifyHandler implements IExcelVerifyHandler<CatInputEntity> {

    private final ThreadLocal<List<CatInputEntity>> threadLocal = new ThreadLocal<>();
    private Map dicMap = new HashMap();

    @Override
    public ExcelVerifyHandlerResult verifyHandler(CatInputEntity cat) {
        StringJoiner joiner = new StringJoiner(",");
        // 根据姓名与手机号判断数据是否重复
        String name = cat.getName();
        String sex = cat.getSex();
        // mock 数据库
//        boolean duplicates = mockTalentDataService.checkForDuplicates(name, sex);
//        if (duplicates) {
//            joiner.add("数据与数据库数据重复");
//        }

        // 字典的校验
        // ...



        List<CatInputEntity> threadLocalVal = threadLocal.get();
        if (threadLocalVal == null) {
            threadLocalVal = new ArrayList<>();
        }

        threadLocalVal.forEach(e -> {
            if (e.equals(cat)) {
                int lineNumber = e.getRowNum() + 1;
                joiner.add("数据与第" + lineNumber + "行重复");
            }
        });
        // 添加本行数据对象到ThreadLocal中
        threadLocalVal.add(cat);
        threadLocal.set(threadLocalVal);
        if (joiner.length() != 0) {
            return new ExcelVerifyHandlerResult(false, joiner.toString());
        }
        return new ExcelVerifyHandlerResult(true);
    }

    public ThreadLocal<List<CatInputEntity>> getThreadLocal() {
        return threadLocal;
    }

    public Map getDicMap() {
        return dicMap;
    }

    public void setDicMap(Map dicMap) {
        this.dicMap = dicMap;
    }
}
